#!/bin/bash
. ./var
. ./Tmpdir
zabbix_agent(){
    #定义zabbix server的IP地址
    zabbix_server=39.107.229.89
    zabbix_agent_version="zabbix-agent-4.4.7-1.el7.x86_64.rpm"

    #调用Tmpdir函数
    Tmpdir
    cd $dir && wget -V &> /dev/null || yum -y install wget
    zabbix_agentd -V &> /dev/null && echoRed "检测到系统中有zabbix-agentd命令，故而退出！" && rm -rf $dir && exit 1
    #wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.6-1.el7.x86_64.rpm
    wget $ip/${zabbix_agent_version} && yum -y install $dir/${zabbix_agent_version}

    #修改相应的配置文件
    [ -f /etc/zabbix/zabbix_agentd.conf ] && sed -i.bak "s/^Server=.*/Server=${zabbix_server}/g"  /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^ServerActive=.*/ServerActive=${zabbix_server}/g"  /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^Hostname=.*/Hostname=$(hostname)/g"  /etc/zabbix/zabbix_agentd.conf
    echo "UnsafeUserParameters=1" >> /etc/zabbix/zabbix_agentd.conf
    #sed -i "s/^UnsafeUserParameters=.*/UnsafeUserParameters=1/"  /etc/zabbix/zabbix_agentd.conf
    #sed -i "s/^Hostname=.*/Hostname=$(hostname -I)/g"  /etc/zabbix/zabbix_agentd.conf
    systemctl enable zabbix-agent && systemctl restart zabbix-agent && systemctl status zabbix-agent
    zabbix_agentd -V &> /dev/null && echoGreen "已完成安装，可尽情享用！" || echoYellow "可能安装有问题，请检查！" 
    rm -rf $dir
}
